# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# vim: set ft=python:

from buildbot.scheduler import Dependent

from common import chromium_utils

from master import gitiles_poller
from master import master_config
from master import master_utils
from master import recipe_master_helper
from master import slaves_list
from master.factory import annotator_factory
from master.factory import chromium_factory

import collections
import config
import master_site_config

ActiveMaster = master_site_config.ChromiumGPU

# This is the dictionary that the buildmaster pays attention to. We also use
# a shorter alias to save typing.
c = BuildmasterConfig = {}
c['status'] = []

# Disable compression for the stdio files.
c['logCompressionLimit'] = False

config.DatabaseSetup(c, require_dbconfig=ActiveMaster.is_production_host)

####### CHANGESOURCES

master_poller = gitiles_poller.GitilesPoller(
    'https://chromium.googlesource.com/chromium/src')

c['change_source'] = [master_poller]


####### SLAVES

# Load the slave list. We need some information from it in order to
# produce the builders.
slaves = slaves_list.SlavesList('slaves.cfg', 'ChromiumGPU')

####### SCHEDULERS

## configure the Schedulers

# Main scheduler for all changes in trunk.

trigger_name_map = recipe_master_helper.AddSchedulersAndTriggers(
    buildmaster_config=c, slave_list=slaves,
    scheduler_name='gpu', branch='master')

####### BUILDERS

builders = []

# ----------------------------------------------------------------------------
# FACTORIES

m_annotator = annotator_factory.AnnotatorFactory()
m_android = chromium_factory.ChromiumFactory(
    'src/build', 'linux2', nohooks_on_update=True, target_os='android')

# Some shortcut to simplify the code below.
F_ANDROID = m_android.ChromiumAnnotationFactory

# TODO(kbr): switch the Android builder to use recipes too.
recipe_master_helper.AddRecipeBasedBuilders(
  c, slaves, m_annotator, trigger_name_map)

f_android_nexus_dbg = F_ANDROID(
    target='Debug',
    slave_type='AnnotatedBuilder',
    annotation_script='src/build/android/buildbot/bb_run_bot.py',
    factory_properties={
      'android_bot_id': 'gpu-builder-tests-dbg',
      'gclient_timeout': 3600
    })

# ----------------------------------------------------------------------------
# BUILDER DEFINITIONS

b_android_nexus5 = {'name': 'Android Debug (Nexus 5)',
                    'factory': f_android_nexus_dbg}
b_android_nexus6 = {'name': 'Android Debug (Nexus 6)',
                    'factory': f_android_nexus_dbg}
b_android_nexus7 = {'name': 'Android Debug (Nexus 7)',
                    'factory': f_android_nexus_dbg}
b_android_nexus9 = {'name': 'Android Debug (Nexus 9)',
                    'factory': f_android_nexus_dbg}

c['builders'] += [ b_android_nexus5,
                   b_android_nexus6,
                   b_android_nexus7,
                   b_android_nexus9 ]

# Associate the slaves to the manual builders. The configuration is in
# slaves.cfg.
for builder in c['builders']:
  builder['slavenames'] = slaves.GetSlavesName(builder=builder['name'])

####### BUILDSLAVES

# The 'slaves' list defines the set of allowable buildslaves. List all the
# slaves registered to a builder. Remove dupes.
c['slaves'] = master_utils.AutoSetupSlaves(c['builders'],
                                           config.Master.GetBotPassword())

# Make sure everything works together.
master_utils.VerifySetup(c, slaves)

####### STATUS TARGETS

# Buildbot master url:
# Must come before AutoSetupMaster().
c['buildbotURL'] = ActiveMaster.buildbot_url

# Adds common status and tools to this master.
master_utils.AutoSetupMaster(c, ActiveMaster,
    public_html='../master.chromium/public_html',
    templates=['../master.chromium/templates'],
    tagComparator=master_poller.comparator,
    enable_http_status_push=ActiveMaster.is_production_host)
